/**
 * Copyright (c) 2008,中企动力华南研发中心 All rights reserved.
 */
package com.ce.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.Map;

import com.sitechasia.webx.core.utils.populator.BasePopulator;

/**
 * 逻辑、功能相关描述:上传工具类辅助类
 *
 * @author 申定溪 编写日期：2008-1-21
 */

public class MultipartRequestBeanPopulator extends BasePopulator {

	public MultipartRequestBeanPopulator() {}

	@SuppressWarnings("unchecked")

	protected boolean doPopulate(Object source, Object target,
								 Map propertiesMapping, String[] ignoreProperties) {

		if ((target instanceof Collection) ||
			(target instanceof MultipartRequest) ||
			(target instanceof ResultSet) || !(source instanceof MultipartRequest))

            return false;

		Map requstMap = ((MultipartRequest)source).getParameterMap();

        if (target instanceof Map)
        {
            ((Map)target).putAll(requstMap);

             return true;
        }

        PropertyDescriptor targetDescriptors[] = (PropertyDescriptor[])null;

        try
        {
            targetDescriptors = Introspector.getBeanInfo(target.getClass()).getPropertyDescriptors();

        } catch(IntrospectionException ie) {

        	logger.debug("Failed on getting bean's properties", ie);
            return false;
        }

        PropertyDescriptor apropertydescriptor[] = targetDescriptors;

        int i = 0;

        for (int j = apropertydescriptor.length; i < j; i++)
        {
            PropertyDescriptor targetDescriptor = apropertydescriptor[i];

            if (!targetDescriptor.getName().equals("class") && doProcess(targetDescriptor.getName(), ignoreProperties)){

            	String sourcePropertyName = null;

                if(propertiesMapping != null && propertiesMapping.containsKey(targetDescriptor.getName()))
                    sourcePropertyName = (String)propertiesMapping.get(targetDescriptor.getName());

                if(sourcePropertyName == null && requstMap.containsKey(targetDescriptor.getName()) || sourcePropertyName != null && !requstMap.containsKey(sourcePropertyName) && requstMap.containsKey(targetDescriptor.getName()))
                    sourcePropertyName = targetDescriptor.getName();

                if(sourcePropertyName != null && sourcePropertyName.length() != 0)
                    try
                    {

                    	String[] reqList = new String []{requstMap.get(sourcePropertyName).toString()};

                    	Object sourceValue = reqList;

                    	Class targetType = targetDescriptor.getPropertyType();

                    	if(sourceValue!=null&&((String[])sourceValue).length == 1)
                        {
                            if(!targetType.isArray())
                            {
                                String tempAssignValue = ((String[])sourceValue)[0];
                                if(tempAssignValue != null)
                                    tempAssignValue = tempAssignValue.trim();
                                sourceValue = tempAssignValue != null && tempAssignValue.length() != 0 ? ((Object) (tempAssignValue)) : null;
                            }
                        } else

                        if(sourceValue!=null&&((String[])sourceValue).length == 0)
                            sourceValue = null;
                        else

                        if(sourceValue!=null&&((String[])sourceValue).length > 1 && !targetType.isArray())
                        {
                            String tempAssignValue = ((String[])sourceValue)[0];
                            if(tempAssignValue != null)
                                tempAssignValue = tempAssignValue.trim();
                            sourceValue = tempAssignValue != null && tempAssignValue.length() != 0 ? ((Object) (tempAssignValue)) : null;
                        }

                        if(sourceValue != null)
                        {
                            Object convertedValue = getConverter().convertValue(sourceValue, targetType);
                            targetDescriptor.getWriteMethod().invoke(target, new Object[] {
                                convertedValue
                            });
                        }
                    }
                    catch(Exception e)
                    {
                        logger.debug("Exception", e);
                    }
            }
        }

        return true;
	}



	protected boolean doPopulate(Object arg0, Object arg1, Map<String, String> arg2, String[] arg3, Object... arg4) {
		// TODO Auto-generated method stub
		return false;
	}

}
